//
//  LoginTextView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/8.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum LoginTextViewType {
    case account
    case password
}

protocol LoginTextViewDelegate: NSObjectProtocol {
    func loginBtnIsEnable()
}

class LoginTextView: UIView {
    
    var type: LoginTextViewType = .account {
        didSet {
            switch type {
            case .account:
                textfield.clearButtonMode = .whileEditing
            case .password:
                textfield.isSecureTextEntry = true
                textfield.rightViewMode = .always
                let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
                let button = UIButton(type: .custom)
                button.frame = rightView.frame
                button.setImage(UIImage(named: "login_eye"), for: .normal)
                button.setImage(UIImage(named: "login_eye_select"), for: .selected)
                button.addTarget(self, action: #selector(passwordPlainTextInput(_:)), for: .touchUpInside)
                rightView.addSubview(button)
                textfield.rightView = rightView
            }
        }
    }
    
    var leftImg: UIImage? {
        didSet {
            imgView.image = leftImg
            textfield.leftView = leftView
        }
    }
    
    var placeholder: String? {
        didSet {
            textfield.placeholder = placeholder
        }
    }
    
    var isEnable: Bool = false
    
    var inputStr: String = "" {
        didSet {
            textfield.text = inputStr
        }
    }
    
    public var keyboardType: UIKeyboardType = .default {
        didSet {
            textfield.keyboardType = keyboardType
        }
    }
    
    weak var delegate: LoginTextViewDelegate?
    
    private lazy var textfield: UITextField = { [unowned self] in
        let tf = UITextField()
        tf.leftViewMode = .always
        tf.delegate = self
        tf.tintColor = .textDark
        tf.textColor = .textDark
        tf.font = .regular(18)
        return tf
    }()
    
    private lazy var leftView = UIView()
    
    private lazy var imgView = UIImageView()
    
    private lazy var splitLine: UIView = {
        let view = UIView()
        view.backgroundColor = .splLineNormal
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        setupUI()
    }
}

extension LoginTextView: UITextFieldDelegate {
    private func setupUI() {
        addSubview(textfield)
        leftView.addSubview(imgView)
        addSubview(splitLine)
        
        textfield.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        splitLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        leftView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
        imgView.frame = CGRect(x: 0, y: 0, width: 18, height: 18)
        
        textfield.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    }
    
    @objc private func passwordPlainTextInput(_ sender: UIButton) {
        sender.isSelected.toggle()
        textfield.isSecureTextEntry = !sender.isSelected
    }
    
    @objc private func textFieldDidChange(_ textField: UITextField) {
        inputStr = textField.text!
        isEnable = textField.text != nil && textField.text != ""
        delegate?.loginBtnIsEnable()
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .lineSelect
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .lineNormal
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
